package com.ruoyi.blogs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blogs.mapper.BsNoteLikeRecordMapper;
import com.ruoyi.blogs.mapper.BsNoteMapper;
import com.ruoyi.blogs.domain.BsNote;
import com.ruoyi.blogs.domain.BsNoteLikeRecord;
import com.ruoyi.blogs.service.IBsNoteLikeRecordService;

/**
 * 文章点赞记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-30
 */
@Service
public class BsNoteNoteLikeRecordServiceImpl implements IBsNoteLikeRecordService
{
    @Autowired
    private BsNoteLikeRecordMapper bsNoteLikeRecordMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private BsNoteMapper bsNoteMapper;

    /**
     * 查询文章点赞记录
     *
     * @param id 文章点赞记录主键
     * @return 文章点赞记录
     */
    @Override
    public BsNoteLikeRecord selectBsNoteLikeRecordById(String id)
    {
        return bsNoteLikeRecordMapper.selectBsNoteLikeRecordById(id);
    }

    /**
     * 查询文章点赞记录列表
     *
     * @param bsNoteLikeRecord 文章点赞记录
     * @return 文章点赞记录
     */
    @Override
    public List<BsNoteLikeRecord> selectBsNoteLikeRecordList(BsNoteLikeRecord bsNoteLikeRecord)
    {
        return bsNoteLikeRecordMapper.selectBsNoteLikeRecordList(bsNoteLikeRecord);
    }

    /**
     * 新增文章点赞记录
     *
     * @param bsNoteLikeRecord 文章点赞记录
     * @return 结果
     */
    @Override
    public int insertBsNoteLikeRecord(BsNoteLikeRecord bsNoteLikeRecord)
    {
        BsNote bsNote =  bsNoteMapper.selectBsNoteById(bsNoteLikeRecord.getNoteId());
        SysUser user = new SysUser();
        user.setUserId(bsNote.getAuthorId());
        user.setLikeNum(0);
        userMapper.updateUserNum(user);
        bsNoteLikeRecord.setCreateTime(DateUtils.getNowDate());
        bsNoteLikeRecord.setUpdateTime(DateUtils.getNowDate());
        bsNoteLikeRecord.setUserId(SecurityUtils.getUserId());
        return bsNoteLikeRecordMapper.insertBsNoteLikeRecord(bsNoteLikeRecord);
    }

    /**
     * 修改文章点赞记录
     *
     * @param bsNoteLikeRecord 文章点赞记录
     * @return 结果
     */
    @Override
    public int updateNoteBsLikeRecord(BsNoteLikeRecord bsNoteLikeRecord)
    {
        bsNoteLikeRecord.setUpdateTime(DateUtils.getNowDate());
        return bsNoteLikeRecordMapper.updateNoteBsLikeRecord(bsNoteLikeRecord);
    }

    /**
     * 批量删除文章点赞记录
     *
     * @param ids 需要删除的文章点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteBsNoteLikeRecordByIds(String[] ids)
    {
        return bsNoteLikeRecordMapper.deleteBsNoteLikeRecordByIds(ids);
    }

    /**
     * 删除文章点赞记录信息
     *
     * @param id 文章点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteBsNoteLikeRecordById(String id)
    {
        return bsNoteLikeRecordMapper.deleteBsNoteLikeRecordById(id);
    }

    @Override
    public int deleteBsNoteLikeRecordByQuery(BsNoteLikeRecord bsNoteLikeRecord) {
        BsNote bsNote =  bsNoteMapper.selectBsNoteById(bsNoteLikeRecord.getNoteId());
        SysUser user = new SysUser();
        user.setUserId(bsNote.getAuthorId());
        user.setLikeNum(1);
        user.setSearchValue(Integer.toString(1));
        userMapper.updateUserNum(user);
        return bsNoteLikeRecordMapper.deleteBsNoteLikeRecordByQuery(bsNoteLikeRecord);
    }
}